package com.probox.common.entity.service.wechat.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.probox.common.core.enums.ApplymentState;
import com.probox.common.core.enums.BankAccountType;
import com.probox.common.core.enums.SalesScenesType;
import com.probox.common.core.enums.SubjectType;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.wechat.PayAudit.AuditResult;
import com.probox.common.entity.domain.wechat.PayAudit.WechatPayAudit;
import com.probox.common.entity.domain.wechat.PayAudit.additionInfo.AdditionInfo;
import com.probox.common.entity.domain.wechat.PayAudit.bankaccountinfo.BankAccountInfo;
import com.probox.common.entity.domain.wechat.PayAudit.businessinfo.BusinessInfo;
import com.probox.common.entity.domain.wechat.PayAudit.businessinfo.salesinfo.MiniProgramInfo;
import com.probox.common.entity.domain.wechat.PayAudit.businessinfo.salesinfo.MpInfo;
import com.probox.common.entity.domain.wechat.PayAudit.businessinfo.salesinfo.SalesInfo;
import com.probox.common.entity.domain.wechat.PayAudit.contactInfo.ContactInfo;
import com.probox.common.entity.domain.wechat.PayAudit.settlementinfo.SettlementInfo;
import com.probox.common.entity.domain.wechat.PayAudit.subject_info.BusinessLicenseInfo;
import com.probox.common.entity.domain.wechat.PayAudit.subject_info.CertificateInfo;
import com.probox.common.entity.domain.wechat.PayAudit.subject_info.OrganizationInfo;
import com.probox.common.entity.domain.wechat.PayAudit.subject_info.SubjectInfo;
import com.probox.common.entity.domain.wechat.PayAudit.subject_info.identityInfo.IdCardInfo;
import com.probox.common.entity.domain.wechat.PayAudit.subject_info.identityInfo.IdentityInfo;
import com.probox.common.entity.domain.wechat.WechatArea;
import com.probox.common.entity.domain.wechat.WechatAuthorizationInfo;
import com.probox.common.entity.domain.wechat.WechatImg;
import com.probox.common.entity.domain.wechat.vo.*;
import com.probox.common.entity.mapper.wechat.WechatAreaMapper;
import com.probox.common.entity.mapper.wechat.WechatImgMapper;
import com.probox.common.entity.mapper.wechat.WechatPayAuditMapper;
import com.probox.common.entity.service.wechat.IWechatAuthorizationInfoService;
import com.probox.common.entity.service.wechat.WechatMiniService;
import com.probox.common.entity.service.wechat.WechatPayAuditService;
import com.probox.common.entity.utils.CertHelper;
import com.probox.common.entity.utils.ImgUploadingUtil;
import com.probox.common.entity.utils.WeChatRequest;
import com.probox.common.redis.service.RedisService;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.crypto.IllegalBlockSizeException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信支付授权审核
 *
 * @author Kite
 * @date 2021/6/29
 */
@Service
@Transactional
public class WechatPayAuditServiceImpl implements WechatPayAuditService {

    @Autowired
    private WechatPayAuditMapper wechatPayAuditMapper;

    @Autowired
    private WechatMiniService wechatMiniService;

    @Autowired
    private IWechatAuthorizationInfoService wechatAuthorizationInfoService;

    private final String applet = "applet";

    private final String allwork = "allwork";

    @Autowired
    private RedisService redisService;

    @Autowired
    private WechatImgMapper wechatImgMapper;

    @Autowired
    private WechatAreaMapper wechatAreaMapper;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    //提交授权地址
    private final static String custom_send_url = "https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/";

    //请求平台证书地址
    private static final String certificatesUrl = "https://api.mch.weixin.qq.com/v3/certificates";

    //图片上传URL
    private static final String uploadUrl = "https://api.mch.weixin.qq.com/v3/merchant/media/upload";
    /**
     * 查询申请状态地址
     */
    private static final String applymentUrl = "https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/applyment_id/";

    //编码
    private static final String charset = "UTF-8";

    /**
     * 新增支付授权单
     *
     * @param wechatPayAudit
     * @return
     */
    @Override
    public String addPayAudit(WechatPayAudit wechatPayAudit, String authorizerAppid) {


        DateTime time = DateUtil.date();
        String businessCode = DateUtil.format(time, "yyyyMMddHHmmssSSS");
        Long incr = redisService.incr(businessCode, 60 * 60);
        String businessCodeStr = businessCode + incr;
        wechatPayAudit.setBusinessCode(businessCodeStr);

        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("business_code", businessCodeStr);
        Integer payAudit = wechatPayAuditMapper.selectCount(wechatPayAuditQueryWrapper);
        if (payAudit > 0) {
            throw new RuntimeException("网络繁忙，请刷新后重试");
        }

        wechatPayAudit.setCreateTime(time);
        wechatPayAudit.setUpdateTime(time);
        wechatPayAudit.setPlatformUid(SecurityUtils.getPlatformId());
        wechatPayAudit.setStep("1");
        wechatPayAuditMapper.insert(wechatPayAudit);


        //新增主体信息类型
        SubjectInfo subjectInfo = new SubjectInfo();
        subjectInfo.setSubject_type(wechatPayAudit.getSubjectType());
        QueryWrapper wechatPayAuditMapperQueryWrapper = new QueryWrapper();
        wechatPayAuditMapperQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditMapperQueryWrapper.eq("business_code", businessCodeStr);
        wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditMapperQueryWrapper);
        subjectInfo.setUbo_info(null);
        //初始化组织机构代码信息
        OrganizationInfo organizationInfo = new OrganizationInfo("", "", "", "");
        subjectInfo.setOrganization_info(organizationInfo);

        //初始化营业执照信息
        BusinessLicenseInfo businessLicenseInfo = new BusinessLicenseInfo("", "", "", "");
        subjectInfo.setBusiness_license_info(businessLicenseInfo);

        //初始化身份证信息
        IdentityInfo id_card_info = new IdentityInfo();
        IdCardInfo idCardInfo = new IdCardInfo("", "");
        //默认未最终受益人
        id_card_info.setOwner(true);
        id_card_info.setId_card_info(idCardInfo);
        subjectInfo.setIdentity_info(id_card_info);

        String res = JSONObject.toJSONString(subjectInfo);
        JSONObject jsonObject = JSONObject.parseObject(res);
        wechatPayAudit.setSubjectInfo(jsonObject.toJSONString());


        //初始化超级管理员信息
        ContactInfo contactInfo = new ContactInfo("", "", "", "", "");

        res = JSONObject.toJSONString(contactInfo);
        JSONObject contactInfoJson = JSONObject.parseObject(res);
        wechatPayAudit.setContactInfo(contactInfoJson.toJSONString());

        wechatPayAudit.setUpdateTime(new Date());


        wechatPayAuditMapper.updateById(wechatPayAudit);

        //新增经营信息类型
        BusinessInfo businessInfo = new BusinessInfo();
        SalesInfo salesInfo = new SalesInfo();

        MiniProgramInfo miniProgramInfo = new MiniProgramInfo();
        miniProgramInfo.setMini_program_appid("");
        miniProgramInfo.setMini_program_sub_appid("");
        salesInfo.setMini_program_info(miniProgramInfo);

        MpInfo mpInfo = new MpInfo();
        mpInfo.setMp_appid("");
        mpInfo.setMp_sub_appid("");
        salesInfo.setMp_info(mpInfo);
        businessInfo.setSales_info(salesInfo);
        addBusinessInfo(businessInfo, wechatPayAudit.getBusinessCode());


        //新增结算账户类型
        BankAccountInfo bankAccountInfo = new BankAccountInfo();
        bankAccountInfo.setBank_account_type(BankAccountType.corporate_account.getKey());
        addBankAccountInfo(bankAccountInfo, wechatPayAudit.getBusinessCode());


        wechatPayAudit.setStep("1");
        wechatPayAuditMapper.updateById(wechatPayAudit);

        //绑定授权单跟微信授权关系
        UpdateWrapper<WechatAuthorizationInfo> updateWrapper = new UpdateWrapper<WechatAuthorizationInfo>();
        updateWrapper.eq("PLATFORM_ID", SecurityUtils.getPlatformId());
        updateWrapper.eq("AUTHORIZER_APPID", authorizerAppid);
        updateWrapper.set("BUSINESS_CODE", wechatPayAudit.getBusinessCode());
        wechatAuthorizationInfoService.update(updateWrapper);


        return businessCodeStr;
    }


    /**
     * 修改授权单
     *
     * @param wechatPayAudit
     */
    @Override
    public Integer updatePayAuditType(WechatPayAudit wechatPayAudit, String businessCode) {
        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit payAudit = wechatPayAuditMapper.selectOne(wechatPayAuditQueryWrapper);
        payAudit.setSubjectType(wechatPayAudit.getSubjectType());
        SubjectInfo subjectInfo = JSONObject.parseObject(payAudit.getSubjectInfo(), SubjectInfo.class);
        subjectInfo.setSubject_type(wechatPayAudit.getSubjectType());
        String res = JSONObject.toJSONString(subjectInfo);
        JSONObject jsonObject = JSONObject.parseObject(res);
        payAudit.setSubjectInfo(JSONObject.toJSONString(jsonObject));
        return wechatPayAuditMapper.updateById(payAudit);

    }

    /**
     * 查询主体
     *
     * @return
     */
    @Override
    public WechatPayAuditVo getSubjectInfo(String businessCode) {
        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditQueryWrapper);
        if (wechatPayAudit == null) {
            return null;
        }
        WechatPayAuditVo wechatPayAuditVo = new WechatPayAuditVo();
        BeanUtils.copyProperties(wechatPayAudit, wechatPayAuditVo);
        //主体资料
        SubjectInfo subjectInfo = JSON.parseObject(wechatPayAudit.getSubjectInfo(), SubjectInfo.class);
        if (subjectInfo.getCertificate_info() == null) {
            CertificateInfo certificateInfo = new CertificateInfo("", "", "", "", "", "", "", "");
            subjectInfo.setCertificate_info(certificateInfo);
        }
        //初始化组织机构代码
        if (subjectInfo.getOrganization_info() == null) {
            OrganizationInfo organizationInfo = new OrganizationInfo("", "", "", "");
            subjectInfo.setOrganization_info(organizationInfo);
        }
        wechatPayAuditVo.setSubjectInfo(subjectInfo);

        return wechatPayAuditVo;
    }

    /**
     * 查询经营资料
     *
     * @return
     */
    @Override
    public WechatPayAuditVo getBusinessInfo(String businessCode) {
        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditQueryWrapper);
        WechatPayAuditVo wechatPayAuditVo = new WechatPayAuditVo();
        BeanUtils.copyProperties(wechatPayAudit, wechatPayAuditVo);
        //查询经营资料
        BusinessInfo businessInfo = JSON.parseObject(wechatPayAudit.getBusinessInfo(), BusinessInfo.class);
        SalesInfo sales_info = businessInfo.getSales_info();
        WechatImgVO wechatImgVO = new WechatImgVO();

        if (sales_info != null) {
            MiniProgramInfo mini_program_info = sales_info.getMini_program_info();
            if (mini_program_info != null) {
                String[] mini_program_pics = mini_program_info.getMini_program_pics();
                if (mini_program_pics != null && mini_program_pics.length > 0) {
                    List<WechatImg> wachatImg = getWachatImg(mini_program_pics);
                    List<String> collect = wachatImg.stream().map(WechatImg::getLocalityUrl).collect(Collectors.toList());
                    mini_program_info.setMini_program_pics(collect.toArray(new String[collect.size()]));
                    String miniProgramPics = "";
                    for (int i = 0; i < collect.size(); i++) {
                        miniProgramPics += collect.get(i);
                        if (i != collect.size() - 1) {
                            miniProgramPics += ",";
                        }
                    }
                    wechatImgVO.setMiniProgramPics(miniProgramPics);
                }
            } else {
                MiniProgramInfo miniProgramInfo = new MiniProgramInfo();
                miniProgramInfo.setMini_program_appid("");
                miniProgramInfo.setMini_program_sub_appid("");
                sales_info.setMini_program_info(miniProgramInfo);
            }
            MpInfo mp_info = sales_info.getMp_info();
            if (mp_info != null) {
                String[] mp_pics = mp_info.getMp_pics();
                if (mp_pics != null && mp_info.getMp_pics().length > 0) {
                    List<WechatImg> wachatImg = getWachatImg(mp_pics);
                    List<String> collect = wachatImg.stream().map(WechatImg::getLocalityUrl).collect(Collectors.toList());
                    String mpPics = "";
                    for (int i = 0; i < collect.size(); i++) {
                        mpPics += collect.get(i);
                        if (i != collect.size() - 1) {
                            mpPics += ",";
                        }
                    }
                    wechatImgVO.setMpPics(mpPics);
                }
            } else {
                MpInfo mpInfo = new MpInfo();
                mpInfo.setMp_appid("");
                mpInfo.setMp_sub_appid("");
                sales_info.setMp_info(mpInfo);
            }


            wechatPayAuditVo.setWechatImgVO(wechatImgVO);
        }
        businessInfo.setSales_info(sales_info);
        wechatPayAuditVo.setBusinessInfo(businessInfo);
        return wechatPayAuditVo;
    }

    /**
     * 获得小程序/公众号ID
     *
     * @return
     */
    @Override
    public HashMap getAppId() {
        HashMap hashMap = new HashMap();
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        hashMap.put("applet", authorizerAppid.getAuthorizerAppid());
        authorizerAppid = wechatMiniService.getAuthorizerAppid(allwork);
        hashMap.put("allwork", authorizerAppid.getAuthorizerAppid());
        return hashMap;
    }

    ;

    /**
     * 查询结算规则
     *
     * @return
     */
    @Override
    public WechatPayAuditVo getSettlementInfo(String businessCode) {
        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditQueryWrapper);
        if (wechatPayAudit == null) {
            return null;
        }
        WechatPayAuditVo wechatPayAuditVo = new WechatPayAuditVo();
        BeanUtils.copyProperties(wechatPayAudit, wechatPayAuditVo);
        WechatImgVO wechatImgVO = new WechatImgVO();
        //结算规则
        SettlementInfo settlementInfo = JSON.parseObject(wechatPayAudit.getSettlementInfo(), SettlementInfo.class);
        if (settlementInfo == null) {
            settlementInfo = new SettlementInfo();
        } else {
            String[] qualifications = settlementInfo.getQualifications();
            if (qualifications != null && qualifications.length > 0) {
                List<WechatImg> wachatImg = getWachatImg(qualifications);
                List<String> collect = wachatImg.stream().map(WechatImg::getLocalityUrl).collect(Collectors.toList());
                String qualificationsVo = "";
                for (int i = 0; i < collect.size(); i++) {
                    qualificationsVo += collect.get(i);
                    if (i != collect.size() - 1) {
                        qualificationsVo += ",";
                    }
                }
                wechatImgVO.setQualifications(qualificationsVo);
            }
        }
        wechatPayAuditVo.setWechatImgVO(wechatImgVO);
        wechatPayAuditVo.setSettlementInfo(settlementInfo);
        return wechatPayAuditVo;
    }

    /**
     * 查询查询银行账户
     *
     * @return
     */
    @Override
    public WechatPayAuditVo getBankAccountInfo(String businessCode) {
        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditQueryWrapper);
        if (wechatPayAudit == null) {
            return null;
        }
        WechatPayAuditVo wechatPayAuditVo = new WechatPayAuditVo();
        BeanUtils.copyProperties(wechatPayAudit, wechatPayAuditVo);
        //结算银行
        BankAccountInfo bankAccountInfo = JSON.parseObject(wechatPayAudit.getBankAccountInfo(), BankAccountInfo.class);
        if (bankAccountInfo != null) {
            wechatPayAuditVo.setBankAccountInfo(bankAccountInfo);
        }
        AdditionInfo additionInfo = JSON.parseObject(wechatPayAudit.getAdditionInfo(), AdditionInfo.class);
        wechatPayAuditVo.setAdditionInfo(additionInfo);

        return wechatPayAuditVo;
    }

    /**
     * 查询超级会员信息
     *
     * @return
     */
    @Override
    public WechatPayAuditVo getContactInfo(String businessCode) {
        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditQueryWrapper);
        if (wechatPayAudit == null) {
            return null;
        }
        WechatPayAuditVo wechatPayAuditVo = new WechatPayAuditVo();
        BeanUtils.copyProperties(wechatPayAudit, wechatPayAuditVo);
        //超级管理员信息
        ContactInfo contactInfo = JSON.parseObject(wechatPayAudit.getContactInfo(), ContactInfo.class);
        wechatPayAuditVo.setContactInfo(contactInfo);
        return wechatPayAuditVo;
    }

    /**
     * 查询支付授权单
     *
     * @return
     */
    @Override
    public WechatPayAuditVo getPayAudit(String businessCode) {
        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditQueryWrapper);
        if (wechatPayAudit == null) {
            return null;
        }
        WechatPayAuditVo wechatPayAuditVo = new WechatPayAuditVo();
        BeanUtils.copyProperties(wechatPayAudit, wechatPayAuditVo);

        if (!wechatPayAuditVo.getAuditState().equals(ApplymentState.uncommittedSubmit.getKey())) {
            getApplyState(businessCode);
        }

        //审核结果
        AuditResult auditResult = JSON.parseObject(wechatPayAudit.getAuditResult(), AuditResult.class);
        wechatPayAuditVo.setAuditResult(auditResult);
        return wechatPayAuditVo;
    }

    /**
     * 提交给微信审核
     *
     * @return
     */
    @Override
    public Integer submitAudit(String businessCode) throws Exception {
        QueryWrapper wechatPayAuditMapperQueryWrapper = new QueryWrapper();
        wechatPayAuditMapperQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditMapperQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditMapperQueryWrapper);

        HashMap<Object, Object> hashMap = new HashMap<>();

        hashMap.put("business_code", wechatPayAudit.getBusinessCode());

        //主体信息加密
        SubjectInfo subjectInfo = JSON.parseObject(wechatPayAudit.getSubjectInfo(), SubjectInfo.class);
        IdentityInfo identity_info = subjectInfo.getIdentity_info();
        IdCardInfo id_card_info = identity_info.getId_card_info();
        id_card_info.setId_card_name(encryption(id_card_info.getId_card_name()));
        id_card_info.setId_card_number(encryption(id_card_info.getId_card_number()));
        subjectInfo.setIdentity_info(identity_info);
        String res = JSONObject.toJSONString(subjectInfo);
        JSONObject jsonObject = JSONObject.parseObject(res);
        hashMap.put("subject_info", JSONObject.parseObject(jsonObject.toJSONString()));


        //超级管理员信息加密
        ContactInfo contactInfo = JSON.parseObject(wechatPayAudit.getContactInfo(), ContactInfo.class);
        contactInfo.setContact_name(encryption(contactInfo.getContact_name()));
        contactInfo.setContact_id_number(encryption(contactInfo.getContact_id_number()));
        contactInfo.setMobile_phone(encryption(contactInfo.getMobile_phone()));
        contactInfo.setContact_email(encryption(contactInfo.getContact_email()));
        res = JSONObject.toJSONString(contactInfo);
        jsonObject = JSONObject.parseObject(res);
        hashMap.put("contact_info", JSONObject.parseObject(jsonObject.toJSONString()));

        //结算银行信息加密
        BankAccountInfo bankAccountInfo = JSON.parseObject(wechatPayAudit.getBankAccountInfo(), BankAccountInfo.class);
        bankAccountInfo.setAccount_name(encryption(bankAccountInfo.getAccount_name()));
        bankAccountInfo.setAccount_number(encryption(bankAccountInfo.getAccount_number()));
        res = JSONObject.toJSONString(bankAccountInfo);
        jsonObject = JSONObject.parseObject(res);
        hashMap.put("bank_account_info", JSONObject.parseObject(jsonObject.toJSONString()));
        //结算规则
        hashMap.put("settlement_info", JSONObject.parseObject(wechatPayAudit.getSettlementInfo()));
        hashMap.put("business_info", JSONObject.parseObject(wechatPayAudit.getBusinessInfo()));
        hashMap.put("addition_info", JSONObject.parseObject(wechatPayAudit.getAdditionInfo()));

        logger.info("--------------------提交参数------------------------------------");
        logger.info("超级管理员信息-》" + hashMap.get("contact_info"));
        logger.info("主体资料-》" + hashMap.get("subject_info"));
        logger.info("经营资料-》" + hashMap.get("business_info"));
        logger.info("结算规则-》" + hashMap.get("settlement_info"));
        logger.info("结算银行账户-》" + hashMap.get("bank_account_info"));
        logger.info("--------------------------------------------------------");

        String body = JSONObject.toJSONString(hashMap);
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(custom_send_url);
        //装填参数
        StringEntity strEntity = new StringEntity(body, charset);
        strEntity.setContentEncoding(charset);
        strEntity.setContentType("application/json");

        //设置参数到请求对象中
        httpPost.setEntity(strEntity);
        String post = WeChatRequest.getAuthorization(custom_send_url, body);
        //指定报文头【Content-type】、【User-Agent】
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Authorization", post);
        String certificates = getCertificates();
        httpPost.addHeader("Wechatpay-Serial", certificates);
        //执行请求操作，并拿到结果（同步阻塞）
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, charset);
        }
        EntityUtils.consume(entity);
        //释放链接
        response.close();
        logger.info("---------------提交结果-----------------------------------------");
        logger.info(body);
        logger.info("---------------提交结果-----------------------------------------");

        try {
            jsonObject = JSONObject.parseObject(body);
            if (jsonObject.getString("code") != null) {
                if (jsonObject.getString("code").equals("PARAM_ERROR")) {

                    throw new BaseException(jsonObject.getString("message"));
                }
            }
        } catch (Exception e) {
            logger.info("提交申请失败,原因:{}", jsonObject.getString("message"));
            String message = jsonObject.getString("message");
            String[] split = message.split("：");
            String[] split1 = message.split("\\(");
            String[] split2 = split1[0].split(":");
            throw new BaseException(split2[0] + ":" + split[split.length - 1]);
        }

        String applyment_id = jsonObject.getString("applyment_id");
        wechatPayAudit.setApplymentId(applyment_id);
        wechatPayAudit.setAuditState(ApplymentState.submitSubmit.getKey());
        wechatPayAuditMapper.updateById(wechatPayAudit);
        return null;
    }


    /**
     * 新增超级管理员信息
     *
     * @param contactInfo 超级管理员信息
     * @return
     */
    @Override
    public Integer addContactInfo(ContactInfo contactInfo, String businessCode) {
        QueryWrapper wechatPayAuditMapperQueryWrapper = new QueryWrapper();
        wechatPayAuditMapperQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditMapperQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditMapperQueryWrapper);

        String res = JSONObject.toJSONString(contactInfo);
        JSONObject jsonObject = JSONObject.parseObject(res);
        wechatPayAudit.setContactInfo(jsonObject.toJSONString());
        wechatPayAudit.setUpdateTime(new Date());
        wechatPayAudit.setStep("6");
        return wechatPayAuditMapper.updateById(wechatPayAudit);
    }


    /**
     * 新增主体资料
     *
     * @param subjectInfo
     * @return
     */
    @Override
    public Integer addSubjectInfo(SubjectInfo subjectInfo, String businessCode) {
        //新增结算银行卡信息
        /**
         * 1、若主体为企业/党政、机关及事业单位/其他组织，可填写：对公银行账户。
         * 2、若主体为个体户，可选择填写：对公银行账户或经营者个人银行卡。
         */
        BankAccountInfo bankAccountInfo = new BankAccountInfo();

        bankAccountInfo.setAccount_name(subjectInfo.getBusiness_license_info().getMerchant_name());
        bankAccountInfo.setBank_account_type(BankAccountType.corporate_account.getKey());
        if (SubjectType.individual_subject.getKey().equals(subjectInfo.getSubject_type())) {
            subjectInfo.setOrganization_info(null);
        }
        addBankAccountInfo(bankAccountInfo, businessCode);

        subjectInfo.setCertificate_info(null);

        //新增、修改超级管理员信息
        /**
         * 超级管理员签约时，校验微信号绑定的银行卡实名信息，是否与该证件号码一致。
         */
        QueryWrapper wechatPayAuditMapperQueryWrapper = new QueryWrapper();
        wechatPayAuditMapperQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditMapperQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditMapperQueryWrapper);

        ContactInfo contactInfo = JSONObject.parseObject(wechatPayAudit.getContactInfo(), ContactInfo.class);
        contactInfo.setContact_id_number(subjectInfo.getIdentity_info().getId_card_info().getId_card_number());
        contactInfo.setContact_name(subjectInfo.getIdentity_info().getId_card_info().getId_card_name());
        addContactInfo(contactInfo, businessCode);

        //身份证信息处理
        IdentityInfo identityInfo = subjectInfo.getIdentity_info();
        //仅设置身份证类型
        identityInfo.setId_doc_type("IDENTIFICATION_TYPE_IDCARD");
        IdCardInfo id_card_info = identityInfo.getId_card_info();

        Date date = DateUtil.parse(id_card_info.getCard_period_begin());
        String format = DateUtil.format(date, "yyyy-MM-dd");
        id_card_info.setCard_period_begin(format);

        date = DateUtil.parse(id_card_info.getCard_period_end());
        format = DateUtil.format(date, "yyyy-MM-dd");
        id_card_info.setCard_period_end(format);

        identityInfo.setId_card_info(id_card_info);
        subjectInfo.setIdentity_info(identityInfo);


        String res = JSONObject.toJSONString(subjectInfo);
        JSONObject jsonObject = JSONObject.parseObject(res);

        wechatPayAudit.setSubjectInfo(jsonObject.toJSONString());
        wechatPayAudit.setUpdateTime(new Date());
        wechatPayAudit.setStep("2");
        return wechatPayAuditMapper.updateById(wechatPayAudit);
    }

    /**
     * 新增经营资料
     *
     * @param businessInfo
     * @return
     */
    @Override
    public Integer addBusinessInfo(BusinessInfo businessInfo, String businessCode) {
        QueryWrapper wechatPayAuditMapperQueryWrapper = new QueryWrapper();
        wechatPayAuditMapperQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditMapperQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditMapperQueryWrapper);


        boolean miniProgram = false;
        boolean officialAccounts = false;
        String[] sales_scenes_type = businessInfo.getSales_info().getSales_scenes_type();
        if (sales_scenes_type != null) {
            for (int i = 0; i < sales_scenes_type.length; i++) {
                if (SalesScenesType.mini_program.getKey().equals(sales_scenes_type[i])) {
                    miniProgram = true;
                }
                if (SalesScenesType.official_accounts.getKey().equals(sales_scenes_type[i])) {
                    officialAccounts = true;
                }
            }
        }

        SalesInfo sales_info = businessInfo.getSales_info();
        //小程序
        MiniProgramInfo mini_program_info = new MiniProgramInfo();
        if (miniProgram) {
            mini_program_info = sales_info.getMini_program_info();
            String[] split = mini_program_info.getMini_program_pics();
            List<WechatImg> wachatImgByQny = getWachatImgByQny(split);
            List<String> collect = wachatImgByQny.stream().map(WechatImg::getMediaId).collect(Collectors.toList());
            split = collect.toArray(new String[collect.size()]);
            mini_program_info.setMini_program_pics(split);
            sales_info.setMini_program_info(mini_program_info);
        } else {
            sales_info.setMini_program_info(null);
        }
        //公众号
        MpInfo mp_info = new MpInfo();
        if (officialAccounts) {
            mp_info = sales_info.getMp_info();
            String[] split = mp_info.getMp_pics();
            List<WechatImg> wachatImgByQny = getWachatImgByQny(split);
            List<String> collect = wachatImgByQny.stream().map(WechatImg::getMediaId).collect(Collectors.toList());
            split = collect.toArray(new String[collect.size()]);
            mp_info.setMp_pics(split);
            sales_info.setMp_info(mp_info);
        } else {
            sales_info.setMp_info(null);
        }


        businessInfo.setSales_info(sales_info);

        String res = JSONObject.toJSONString(businessInfo);
        JSONObject jsonObject = JSONObject.parseObject(res);
        wechatPayAudit.setBusinessInfo(jsonObject.toJSONString());
        wechatPayAudit.setUpdateTime(new Date());
        wechatPayAudit.setStep("3");


        return wechatPayAuditMapper.updateById(wechatPayAudit);
    }


    /**
     * 添加结算规则
     *
     * @param settlementInfo
     * @return
     */
    @Override
    public Integer addSettlementInfo(SettlementInfo settlementInfo, String businessCode) {
        QueryWrapper wechatPayAuditMapperQueryWrapper = new QueryWrapper();
        wechatPayAuditMapperQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditMapperQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditMapperQueryWrapper);

        String[] split = settlementInfo.getQualifications();
        List<WechatImg> wachatImgByQny = getWachatImgByQny(split);
        if (wachatImgByQny != null) {
            List<String> collect = wachatImgByQny.stream().map(WechatImg::getMediaId).collect(Collectors.toList());
            split = collect.toArray(new String[collect.size()]);
            settlementInfo.setQualifications(split);
        }


        String res = JSONObject.toJSONString(settlementInfo);
        JSONObject jsonObject = JSONObject.parseObject(res);
        wechatPayAudit.setSettlementInfo(jsonObject.toJSONString());
        wechatPayAudit.setUpdateTime(new Date());
        wechatPayAudit.setStep("4");
        return wechatPayAuditMapper.updateById(wechatPayAudit);
    }

    /**
     * 新增结算银行账户
     *
     * @param bankAccountInfo
     * @return
     */
    @Override
    public Integer addBankAccountInfo(BankAccountInfo bankAccountInfo, String businessCode) {
        QueryWrapper wechatPayAuditMapperQueryWrapper = new QueryWrapper();
        wechatPayAuditMapperQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditMapperQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditMapperQueryWrapper);
        String res = JSONObject.toJSONString(bankAccountInfo);
        JSONObject jsonObject = JSONObject.parseObject(res);
        wechatPayAudit.setBankAccountInfo(jsonObject.toJSONString());
        wechatPayAudit.setUpdateTime(new Date());
        wechatPayAudit.setStep("5");
        return wechatPayAuditMapper.updateById(wechatPayAudit);
    }

    /**
     * 新增补充材料
     *
     * @param additionInfo
     * @return
     */
    @Override
    public Integer addAdditionInfo(AdditionInfo additionInfo, String businessCode) {
        QueryWrapper wechatPayAuditMapperQueryWrapper = new QueryWrapper();
        wechatPayAuditMapperQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditMapperQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditMapperQueryWrapper);
        String res = JSONObject.toJSONString(additionInfo);
        JSONObject jsonObject = JSONObject.parseObject(res);
        wechatPayAudit.setAdditionInfo(jsonObject.toJSONString());
        wechatPayAudit.setUpdateTime(new Date());
        return wechatPayAuditMapper.updateById(wechatPayAudit);
    }

    /**
     * 微信图片上传
     *
     * @param file   文件
     * @param qnyUrl 七牛地址
     * @return
     * @throws Exception
     */
    @Override
    public String uploadingImg(File file, String qnyUrl) throws Exception {
        JSONObject jsonObject = ImgUploadingUtil.imgUploading(file, uploadUrl);

        WechatImg wechatImg = new WechatImg();
        wechatImg.setCreateTime(new Date());
        wechatImg.setMediaId(jsonObject.getString("media_id"));
        wechatImg.setLocalityUrl(qnyUrl);
        wechatImgMapper.insert(wechatImg);
        return jsonObject.getString("media_id");
    }

    /**
     * 查询微信图片信息
     *
     * @param mediaIds
     * @return
     */
    @Override
    public List<WechatImg> getWachatImg(String[] mediaIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("media_id", mediaIds);
        List list = wechatImgMapper.selectList(queryWrapper);
        return list;
    }


    /**
     * 查询微信图片信息
     *
     * @param localityUrl 七牛云地址
     * @return
     */
    public List<WechatImg> getWachatImgByQny(String[] localityUrl) {
        if (localityUrl == null || localityUrl.length == 0) {
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("locality_url", localityUrl);
        List<WechatImg> list = wechatImgMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 查询微信地址
     *
     * @return
     */
    @Override
    public List getWechatArea(String addCitycode) {
        QueryWrapper<WechatArea> wechatAreaQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(addCitycode)) {
            wechatAreaQueryWrapper.eq("ADD_CITYCODE", 000000);
        } else {
            wechatAreaQueryWrapper.eq("ADD_CITYCODE", addCitycode);
        }
        return wechatAreaMapper.selectList(wechatAreaQueryWrapper);
    }

    /**
     * 查询微信地址
     *
     * @return
     */
    @Override
    public WechatArea getAddCitycode(String addCitycode) {
        QueryWrapper<WechatArea> wechatAreaQueryWrapper = new QueryWrapper<>();
        wechatAreaQueryWrapper.eq("ADD_CODE", addCitycode);
        return wechatAreaMapper.selectOne(wechatAreaQueryWrapper);
    }

    /**
     * 查询申请状态
     *
     * @return
     */
    @Override
    public AuditResult getApplyState(String businessCode) {
        QueryWrapper wechatPayAuditMapperQueryWrapper = new QueryWrapper();
        wechatPayAuditMapperQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditMapperQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditMapperQueryWrapper);
        String url = applymentUrl + wechatPayAudit.getApplymentId();
        logger.info("查询支付授权单单号：{}", wechatPayAudit.getApplymentId());
        logger.info("请求路径：{}", url);
        WeChatRequest weChatRequest = new WeChatRequest();
        String data = weChatRequest.weChatGet(url);
        logger.info("查询支付授权单结果：{}", JSONObject.parseObject(data));
        AuditResult auditResult = JSON.parseObject(data, AuditResult.class);

        if (ApplymentState.applymentStateFinished.getKey().equals(auditResult.getApplyment_state())) {

//            QueryWrapper<WechatAuthorizationInfo> queryWrapper = new QueryWrapper<WechatAuthorizationInfo>();
//            queryWrapper.eq("PLATFORM_ID", SecurityUtils.getPlatformId());
//            queryWrapper.eq("")

            //绑定授权单跟微信授权关系
//            UpdateWrapper<WechatAuthorizationInfo> updateWrapper = new UpdateWrapper<WechatAuthorizationInfo>();
//            updateWrapper.eq("PLATFORM_ID", SecurityUtils.getPlatformId());
//            updateWrapper.eq("BUSINESS_CODE", auditResult.getBusiness_code());
//            updateWrapper.set("MERCHANT_ID", auditResult.getSub_mchid());
//            wechatAuthorizationInfoService.update(updateWrapper);
        }
        String res = JSONObject.toJSONString(auditResult);
        JSONObject jsonObject = JSONObject.parseObject(res);
        wechatPayAudit.setAuditResult(jsonObject.toJSONString());
        wechatPayAudit.setAuditState(auditResult.getApplyment_state());
        wechatPayAuditMapper.updateById(wechatPayAudit);
        return auditResult;
    }


//    /**
//     * 查询支付授权状态
//     *
//     * @return
//     */
//    @Override
//    public AuditResult getPayAuditStatus() {
//        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
//        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
//        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditQueryWrapper);
//        AuditResult auditResult = new AuditResult();
//        if (wechatPayAudit == null || wechatPayAudit.getAuditResult() == null) {
//            auditResult.setApplyment_state(ApplymentState.uncommittedSubmit.getKey());
//            return auditResult;
//        }
//        auditResult = JSON.parseObject(wechatPayAudit.getAuditResult(), AuditResult.class);
//        if (auditResult == null || auditResult.getApplyment_state() == null) {
//            auditResult = new AuditResult(ApplymentState.submitSubmit.getKey());
//        }
//        return auditResult;
//    }


    /**
     * 查询申请单状态
     *
     * @return
     */
    @Override
    public CommercialDataVo getCommercialData(String businessCode) {
        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditQueryWrapper.eq("business_code", businessCode);
        WechatPayAudit wechatPayAudit = wechatPayAuditMapper.selectOne(wechatPayAuditQueryWrapper);
        CommercialDataVo commercialDataVo = new CommercialDataVo(null, null, null, "0", 0, 0);
        if (wechatPayAudit != null) {
            commercialDataVo.setAuditState(wechatPayAudit.getAuditState());
            commercialDataVo.setBusinessCode(wechatPayAudit.getBusinessCode());
            BusinessInfo businessInfo = JSON.parseObject(wechatPayAudit.getBusinessInfo(), BusinessInfo.class);
            commercialDataVo.setMerchantShortname(businessInfo.getMerchant_shortname());

//            AuditResult payAuditStatus = getPayAuditStatus();
//            commercialDataVo.setAuditState(payAuditStatus.getApplyment_state());
            SalesInfo sales_info = businessInfo.getSales_info();
            String[] sales_scenes_type = sales_info.getSales_scenes_type();
            AuditResult applyState = getApplyState(businessCode);
            commercialDataVo.setAuditResult(applyState);
            commercialDataVo.setSubMchid(applyState.getSub_mchid());
            if (sales_scenes_type != null) {
                for (int i = 0; i < sales_scenes_type.length; i++) {
                    if (SalesScenesType.official_accounts.getKey().equals(sales_scenes_type[i])) {
                        commercialDataVo.setMpInfo(1);
                    }
                    if (SalesScenesType.mini_program.getKey().equals(sales_scenes_type[i])) {
                        commercialDataVo.setApplet(1);
                    }
                }
            }
        }
        if (commercialDataVo.getAuditResult() == null) {
            AuditResult auditResult = new AuditResult();
            auditResult.setSign_url("http://resource.tkmall.tongkeer.com/shop/maokun/a6114fc81b8d4b47a11964e1d6e9081d_210805091557");
            commercialDataVo.setAuditResult(auditResult);
        }

        return commercialDataVo;
    }

    /**
     * 查询申请列表
     *
     * @return
     */
    @Override
    public List<WechatPayAuditListVo> wechatPayAuditList(String type) {
        QueryWrapper<WechatPayAudit> wechatPayAuditQueryWrapper = new QueryWrapper<>();
        wechatPayAuditQueryWrapper.eq("platform_uid", SecurityUtils.getPlatformId());
        wechatPayAuditQueryWrapper.eq("audit_state", ApplymentState.applymentStateFinished.getKey());
        List<WechatPayAudit> wechatPayAudits = wechatPayAuditMapper.selectList(wechatPayAuditQueryWrapper);
        List<WechatPayAuditListVo> collect = wechatPayAudits.stream().map(o -> {
            WechatPayAuditListVo wechatPayAuditListVo = new WechatPayAuditListVo();
            BusinessInfo businessInfo = JSON.parseObject(o.getBusinessInfo(), BusinessInfo.class);
            SalesInfo sales_info = businessInfo.getSales_info();
            String[] sales_scenes_type = sales_info.getSales_scenes_type();
            boolean equals = false;
            String[] scene = new String[sales_scenes_type.length];
            String sceneStr = "";
            for (int i = 0; i < sales_scenes_type.length; i++) {
                if (!equals) {
                    equals = sales_scenes_type[i].equals(type);
                }
                if (sales_scenes_type[i].equals(SalesScenesType.official_accounts.getKey())) {
                    scene[i] = SalesScenesType.official_accounts.getValue();
                    sceneStr = sceneStr + SalesScenesType.official_accounts.getValue();
                }
                if (sales_scenes_type[i].equals(SalesScenesType.mini_program.getKey())) {
                    scene[i] = SalesScenesType.mini_program.getValue();
                    sceneStr = sceneStr + SalesScenesType.mini_program.getValue();
                }
                if (i != sales_scenes_type.length -1){
                    sceneStr = sceneStr +"、";
                }
            }
            if (!equals) {
                wechatPayAuditListVo.setSelect("0");
            } else {
                wechatPayAuditListVo.setSelect("1");
            }
            wechatPayAuditListVo.setScene(scene);
            AuditResult auditResult = JSON.parseObject(o.getAuditResult(), AuditResult.class);
            SubjectInfo subjectInfo = JSON.parseObject(o.getSubjectInfo(), SubjectInfo.class);
            wechatPayAuditListVo.setSubMchid(auditResult.getSub_mchid());
            wechatPayAuditListVo.setSubjectType(o.getSubjectType());
            wechatPayAuditListVo.setBusinessCode(o.getBusinessCode());
            wechatPayAuditListVo.setCreateTime(o.getCreateTime());
            wechatPayAuditListVo.setMerchantName(subjectInfo.getBusiness_license_info().getMerchant_name());
            wechatPayAuditListVo.setAuditState(ApplymentState.applymentStateFinished.getValue());
            wechatPayAuditListVo.setSceneStr(sceneStr);
            return wechatPayAuditListVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 切换授权单
     *
     * @param vo
     * @return
     */
    @Override
    public Boolean switchPayAudit(QuerySwitchPayAuditVo vo) {
        //绑定授权单跟微信授权关系
        UpdateWrapper<WechatAuthorizationInfo> updateWrapper = new UpdateWrapper<WechatAuthorizationInfo>();
        updateWrapper.eq("PLATFORM_ID", SecurityUtils.getPlatformId());
        updateWrapper.eq("AUTHORIZER_APPID", vo.getAuthorizerAppid());
        updateWrapper.eq("AUTHORIZER_TYPE", vo.getAuthorizerType());
        updateWrapper.set("BUSINESS_CODE", vo.getBusinessCode());
        updateWrapper.set("MERCHANT_ID", vo.getSubMchid());

        return wechatAuthorizationInfoService.update(updateWrapper);
    }


    /**
     * 敏感信息加密
     *
     * @param message
     * @return
     * @throws IOException
     * @throws IllegalBlockSizeException
     */
    public String encryption(String message) {
        if (StringUtils.isEmpty(message)) {
            return null;
        }
        CertHelper certHelper = new CertHelper();
        X509Certificate createPKCS12 = null;
        try {
            String platformCertificate = certHelper.getPlatformCertificate();
            createPKCS12 = certHelper.fromString(platformCertificate);
            return CertHelper.rsaEncryptOAEP(message, createPKCS12);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取平台证书序列号
     *
     * @return
     */
    public String getCertificates() {
        WeChatRequest weChatRequest = new WeChatRequest();
        String body = weChatRequest.weChatGet(certificatesUrl);
        //获取平台证书信息（信息未解密）
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONArray data = jsonObject.getJSONArray("data");
        JSONObject o = (JSONObject) data.get(0);
        body = (String) o.get("serial_no");
        return body;
    }


}
